<?php
/**
 * Created by PhpStorm.
 * User: 魏爷
 * Date: 2016/5/29
 * Time: 23:07
 */

namespace Home\Model;


class BorrowModel extends BaseModel
{
    // 数据表名（不包含表前缀）
    protected $tableName        =   'borrow';

    /**
     * 借阅状态修改【归还请求】
     * @return boolean
    **/
    public function setBorrowIfback($borrow_id){
        $res=$this->where("borrow_id=$borrow_id")->setField('borrow_ifback',3);
        return $res==false?false:true;
    }

    /**
     * 查询所有借阅信息
     * @return array()
    **/
    public function getBorrowAllInfo($u_id){
        return $this->where("u_id=$u_id")->order('borrow_id DESC')->select();
    }
    /**
     * 根据用户ID查询借阅信息
     * @return array()
    **/
    public function getBorrowInfoById($u_id){
        $where=array();
        $where['u_id']=$u_id;
        $where['borrow_ifback']=array('neq',1);
        return $this->where($where)->order('borrow_id DESC')->select();
    }
    /**
     * 借阅书籍数量查询
     * @param Integer $u_id 用户ID
     * @return Integer 数量
    **/
    public function getBorrowByIdBook($u_id){
        $where=array();
        $where['u_id']=$u_id;
        $where['borrow_ifback']=array('neq',1);
        return count($this->where($where)->select());
    }
    /**
     * 借阅信息添加
     * @param Integer $u_id 用户ID
     * @param Integer $book_id 书籍ID
     * @return boolean
    **/
    public function setBorrowBook($u_id,$book_id){
        $data['u_id']=$u_id;
        $data['book_id']=$book_id;
        $data['borrow_time']=time();
        $data['borrow_backtime']=time()+30*24*3600;
        $data['borrow_ifback']=2;
        $res=$this->add($data);
        if($res>0){
            return true;
        }else{
            return false;
        }
    }
}